package com.lovo.project.servlet.user;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.UserDO;
import com.lovo.project.service.UserService;
import com.lovo.project.service.impl.UserServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @title 按条件查询用户
 * @author: luo
 * @date: 2020/11/4 17:28
 */
@WebServlet(name = "FindUserByInfoServlet",urlPatterns = "/user/findUserByInfo")
public class FindUserByInfoServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        System.out.println(page+"    -----    "+size);
        PageHelper.startPage(page == null?1:Integer.parseInt(page),size == null? 5:Integer.parseInt(size));
        String select = request.getParameter("select");

        String info = request.getParameter("info");
        PrintWriter out = response.getWriter();
        try {
            List<UserDO> list = userService.listByInfo(select, info);
            PageInfo pageInfo = PageInfo.of(list);
            out.print(ResultVO.success().add(pageInfo).toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(UserCodeEnum.USER_UNKNOWN_ERROR).toJson());
        }finally {
            out.close();
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
